load("//bazel:macros.bzl", "py_binary")
load("//bazel:skia_rules.bzl", "skia_cc_library", "skia_objc_library")

package(
    default_applicable_licenses = ["//:license"],
)

licenses(["notice"])

exports_files([
    "ToolUtils.h",
    "ToolUtils.cpp",
    "SkMetaData.h",
    "SkMetaData.cpp",
    "Registry.h",
    "Resources.h",
    "Resources.cpp",
    "ResourceFactory.h",
])

skia_cc_library(
    name = "registry",
    hdrs = ["Registry.h"],
    visibility = [
        "//bench:__pkg__",
        "//gm:__pkg__",
        "//tests:__subpackages__",
        "//tools/testrunners/benchmark:__pkg__",
        "//tools/viewer:__pkg__",
    ],
    deps = ["//:core"],
)

skia_cc_library(
    name = "hash_and_encode",
    testonly = True,
    srcs = ["HashAndEncode.cpp"],
    hdrs = ["HashAndEncode.h"],
    visibility = [
        "//dm:__pkg__",
        "//gm:__pkg__",
    ],
    deps = [
        "//:core",
        "//:png_encode_codec",
        "//tools/flags:cmd_flags",
    ],
)

py_binary(
    name = "embed_resources",
    srcs = ["embed_resources.py"],
    visibility = ["//:__subpackages__"],
)

skia_cc_library(
    name = "autorelease_pool",
    hdrs = ["AutoreleasePool.h"],
    visibility = [
        "//bench:__pkg__",
        "//dm:__pkg__",
        "//tools/gpu:__pkg__",
        "//tools/testrunners/benchmark:__pkg__",
    ],
    deps = ["//src/base"],
)

skia_objc_library(
    name = "autorelease_pool_objc",
    srcs = ["AutoreleasePool.mm"],
    hdrs = ["AutoreleasePool.h"],
    visibility = ["//tools/gpu:__pkg__"],
    deps = ["//src/base"],
)

skia_cc_library(
    name = "url_data_manager",
    srcs = ["UrlDataManager.cpp"],
    hdrs = ["UrlDataManager.h"],
    visibility = [
        "//dm:__pkg__",
        "//tools/debugger:__pkg__",
    ],
    deps = ["//:core"],
)

skia_cc_library(
    name = "ddl_utils",
    testonly = True,
    srcs = [
        "DDLPromiseImageHelper.cpp",
        "DDLPromiseImageHelper.h",
        "DDLTileHelper.cpp",
        "DDLTileHelper.h",
    ],
    hdrs = [
        "DDLPromiseImageHelper.h",
        "DDLTileHelper.h",
    ],
    visibility = ["//dm:__pkg__"],
    deps = [
        "//:core",
        "//src/codec:codec_support_priv",
        "//src/gpu/ganesh:ganesh_TEST_UTIL",
    ],
)

skia_cc_library(
    name = "test_font_data_provider",
    testonly = True,
    srcs = ["TestFontDataProvider.cpp"],
    hdrs = ["TestFontDataProvider.h"],
    visibility = ["//dm:__pkg__"],
    deps = [
        "//:core",
        "//modules/jsonreader",
        "//tools/flags:cmd_flags",
    ],
)

skia_cc_library(
    name = "sk_sharing_proc",
    srcs = ["SkSharingProc.cpp"],
    hdrs = ["SkSharingProc.h"],
    visibility = ["//tools/debugger:__pkg__"],
    deps = [
        "//:core",
        "//:png_decode_codec",
        "//:png_encode_codec",
    ],
)

skia_cc_library(
    name = "mskp_player",
    testonly = True,
    srcs = ["MSKPPlayer.cpp"],
    hdrs = ["MSKPPlayer.h"],
    visibility = ["//tools/viewer:__pkg__"],
    deps = [
        ":sk_sharing_proc",
        "//src/gpu/ganesh:ganesh_TEST_UTIL",
        "//src/utils:multi_picture_document",
    ],
)

skia_cc_library(
    name = "runtime_blend_utils",
    srcs = ["RuntimeBlendUtils.cpp"],
    hdrs = ["RuntimeBlendUtils.h"],
    visibility = [
        "//dm:__pkg__",
        "//tests:__pkg__",
        "//tools/viewer:__pkg__",
    ],
    deps = ["//:core"],
)

skia_cc_library(
    name = "stats",
    srcs = ["ProcStats.cpp"],
    hdrs = [
        "ProcStats.h",
        "Stats.h",
    ],
    visibility = [
        "//bench:__pkg__",
        "//dm:__pkg__",
        "//tools/testrunners/benchmark:__pkg__",
    ],
    deps = ["//:core"],
)

skia_cc_library(
    name = "tool_utils",
    testonly = True,
    srcs = [
        "SkMetaData.cpp",
        "ToolUtils.cpp",
    ],
    hdrs = [
        "SkMetaData.h",
        "ToolUtils.h",
    ],
    visibility = [
        "//bench:__subpackages__",
        "//gm:__subpackages__",
        "//tools:__subpackages__",
    ],
    deps = [
        "//:core",
        "//src/base",
        "//src/core:core_priv",
    ],
)

skia_cc_library(
    name = "codec_utils",
    testonly = True,
    srcs = [
        "DecodeUtils.cpp",
        "EncodeUtils.cpp",
    ],
    hdrs = [
        "CodecUtils.h",
        "DecodeUtils.h",
        "EncodeUtils.h",
    ],
    visibility = [
        "//dm:__pkg__",
        "//gm:__subpackages__",
        "//tools/viewer:__pkg__",
    ],
    deps = [
        "//:core",
        "//:png_decode_codec",
        "//:png_encode_codec",
        "//src/base",
        "//src/core:core_priv",
        "//tools:resources",
    ],
)

skia_cc_library(
    name = "resources",
    testonly = True,
    srcs = [
        "Resources.cpp",
    ],
    hdrs = [
        "Resources.h",
    ],
    visibility = [
        "//bench:__subpackages__",
        "//tools:__subpackages__",
    ],
    deps = [
        "//:core",
        "//src/base",
        "//src/core:core_priv",
        "//tools/flags:cmd_flags",
    ],
)

skia_cc_library(
    name = "gpu_tool_utils",
    testonly = True,
    hdrs = ["GpuToolUtils.h"],
    visibility = [
        "//dm:__pkg__",
        "//tools:__subpackages__",
    ],
    deps = [
        "//:core",
    ],
)

# Regenerate workarounds with `bazel run //tools:generate_workarounds`
py_binary(
    name = "generate_workarounds",
    srcs = [":generate_workarounds.py"],
    args = [
        "--output-file",
        "include/gpu/ganesh/GrDriverBugWorkaroundsAutogen.h",
        "src/gpu/gpu_workaround_list.txt",
    ],
    data = [
        ":build_workaround_header",
    ],
    tags = ["no-remote"],
)

py_binary(
    name = "build_workaround_header",
    srcs = ["build_workaround_header.py"],
)

_GENERATE_WORKAROUNDS = """
import os
import subprocess
import sys

# https://bazel.build/docs/user-manual#running-executables
# Note: Bazel eats single quotes, so we must use double quotes.
os.chdir(os.environ["BUILD_WORKSPACE_DIRECTORY"])

# execpath returns the path to the given label relative to the Skia root.
# This will be something like:
#   bazel-out/k8-opt-exec-81C6BA4F/bin/tools/build_workaround_header
# https://bazel.build/reference/be/make-variables#predefined_label_variables
generate_script = os.path.abspath("$(execpath :build_workaround_header)")

result = subprocess.run(
    [generate_script] + sys.argv[1:], capture_output=True, encoding="utf-8")
if result.returncode != 0:
    print(result.stdout)
    print(result.stderr)
    sys.exit(result.returncode)
"""

genrule(
    name = "create_generate_workarounds_script",
    outs = ["generate_workarounds.py"],
    cmd = "echo '%s' > $@" % _GENERATE_WORKAROUNDS,
    tools = [":build_workaround_header"],
)

skia_cc_library(
    name = "full_build",
    testonly = True,
    srcs = [
        # These are *not* skia_cc_library targets because we'd have to set the deps
        # for them, which is redundent. Thus we just compile them as a list of files
        # and the deps specified below should be sufficient for them.
        "//include/core:headers_to_compile",
        "//include/effects:headers_to_compile",
        "//include/encode:headers_to_compile",
        "//include/gpu/ganesh:headers_to_compile",
        "//include/gpu:headers_to_compile",
        "//include/gpu/vk:headers_to_compile",
        "//include/private/base:headers_to_compile",
        "//include/private/chromium:headers_to_compile",
        "//include/private/gpu/ganesh:headers_to_compile",
        "//include/private:headers_to_compile",
        "//modules/svg/src:headers_to_compile",
        "//src/base:headers_to_compile",
        "//src/codec:headers_to_compile",
        "//src/core:headers_to_compile",
        "//src/encode:headers_to_compile",
        "//src/gpu/ganesh/effects:headers_to_compile",
        "//src/gpu/ganesh/geometry:headers_to_compile",
        "//src/gpu/ganesh/gl:headers_to_compile",
        "//src/gpu/ganesh/mock:headers_to_compile",
        "//src/gpu/ganesh/ops:headers_to_compile",
        "//src/gpu/ganesh/tessellate:headers_to_compile",
        "//src/gpu/ganesh:headers_to_compile",
        "//src/gpu/graphite/geom:headers_to_compile",
        "//src/gpu/tessellate:headers_to_compile",
        "//src/gpu/vk:headers_to_compile",
        "//src/gpu:headers_to_compile",
        "//src/pdf:headers_to_compile",
        "//include/docs:headers_to_compile",
    ],
    copts = [
        # Since we are generating .cpp files for headers, some variables/functions which
        # were properly declared static could be mis-interpreted to be unused. We don't
        # want to fail on that. Therefore, we do not want warnings about those unused
        # things to be flagged.
        "-Wno-unused-function",
        "-Wno-unused-template",
        "-Wno-unused-const-variable",
        "-Wno-invalid-constexpr",
    ] + select({
        "@platforms//os:windows": [
            "-D_CRT_USE_BUILTIN_OFFSETOF",
        ],
        "//conditions:default": [],
    }),
    # This is not intended to be used for tests or anything, just for IWYU
    visibility = ["//visibility:private"],
    deps = [
        "//:bmp_decode_codec",
        "//:core",
        "//:fontmgr_data_freetype",
        "//:fontmgr_directory_freetype",
        "//:fontmgr_empty_freetype",
        "//:gif_decode_codec",
        "//:ico_decode_codec",
        "//:jpeg_decode_codec",
        "//:jpeg_encode_codec",
        "//:jpegxl_decode_codec",
        "//:pathops",
        "//:pdf_writer",
        "//:png_decode_codec",
        "//:png_encode_codec",
        "//:skparagraph_harfbuzz_skunicode",
        "//:skresources",
        "//:skshaper_core",
        "//:skshaper_harfbuzz",
        "//:skshaper_unicode",
        "//:skunicode_core",
        "//:skunicode_icu",
        "//:svg_renderer",
        "//:svg_writer",
        "//:wbmp_decode_codec",
        "//:webp_decode_codec",
        "//:webp_encode_codec",
        "//modules/jsonreader",
        "//src/gpu/ganesh/gl:ganesh_gl_TEST_UTIL",
        "//src/gpu/ganesh/vk:ganesh_vulkan_TEST_UTIL",
        "//src/gpu/graphite/vk:graphite_native_vulkan_TEST_UTIL",
        "//tools/sksltrace:sksltraceutils",
    ],
)
